//app.js
var config = require('./config');
var util = require('./utils/util.js');
App({
  // 小程序启动之后 触发
  onLaunch: function () {
    var self = this;
    wx.getSystemInfo({
      success: function (res) {
        self.globalData.clientHeight = res.windowHeight;
        self.globalData.clientWidth = res.windowWidth;
      }
    });
  },
  getUserInfo: function (cb) {
    var that = this;
    if (this.globalData.userInfo) {
      typeof cb == "function" && cb(this.globalData.userInfo);
    } else {
      util.showBusy('正在加载数据');
      // 调用登录接口
      wx.login({
        success: function (resLogin) {
          if (resLogin.code) {
            wx.getUserInfo({
              success: function (res) {
                that.globalData.userInfo = res.userInfo;
                wx.request({
                  url: config.service.user_data,
                  data: { name: res.userInfo.nickName, img: res.userInfo.avatarUrl, code: resLogin.code },
                  method: 'POST',
                  success: function (res1) {
                    that.globalData.testUser = res1.data.code;
                    typeof cb == "function" && cb(that.globalData.userInfo);
                  }
                });
              },
              fail: function () {
                typeof cb == "function" && cb({});
              }
            });
          } else {
            typeof cb == "function" && cb({});
          }
        },
        fail: function () {
          typeof cb == "function" && cb({});
        }
      });
    }
  },
  globalData: {
    userInfo: null,
    cardInfo: null,
    clientHeight: null,
    clientWidth: null,
    testUser: false
  }
})